package com.jrmf.redenvelopes.order.util;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

public class PageTask implements Runnable{
    private CountDownLatch countDownLatch;
    private Sheet sheet;
    private String[] title;
    private CellStyle style;
    private int b;
    private String[][] values;
    private List<Map<String,Object>> accountList;
    public PageTask(CountDownLatch countDownLatch, Sheet sheet, String[] title, CellStyle style, int b, String[][] values, List<Map<String,Object>> accountList) {
        this.countDownLatch = countDownLatch;
        this.sheet = sheet;
        this.title = title;
        this.style = style;
        this.b = b;
        this.values = values;
        this.accountList=accountList;
    }
    @Override
    public void run() {
        try {
            Row row = sheet.createRow(0);
            Cell cell = null;
            for (int i = 0; i < title.length; i++) {
                cell = row.createCell(i);
                cell.setCellValue(title[i]);
                cell.setCellStyle(style);
            }
            /*for (int i = 0; i < b; i++) {
                row = sheet.createRow(i + 1);
                for (int j = 0; j < values[i].length; j++) {
                    row.createCell(j).setCellValue(values[i][j]);
                }
            }*/
            for (int i = 0; i < accountList.size(); i++) {
                row = sheet.createRow(i + 1);
                for (int j = 0; j < 4; j++) {//todo
                    if (j==0){
                        row.createCell(j).setCellValue(String.valueOf(accountList.get(i).get("accountType")));
                    }
                    else if (j==1){
                        row.createCell(j).setCellValue(String.valueOf(accountList.get(i).get("tradeTime")));
                    }
                    else if (j==2){
                        row.createCell(j).setCellValue(String.valueOf(accountList.get(i).get("tradeId")));
                    }
                    else if (j==3){
                        if ("2".equals(accountList.get(i).get("accountType").toString())||"3".equals(accountList.get(i).get("accountType").toString())){
                            row.createCell(j).setCellValue(Double.valueOf(accountList.get(i).get("price").toString())/100);
                        }
                        else {
                            row.createCell(j).setCellValue(String.valueOf(accountList.get(i).get("price")));
                        }
                    }
                    else if (j==4){
                        if (null!=accountList.get(i).get("payReturnStatus") &&!"".equals(accountList.get(i).get("payReturnStatus").toString()) ){
                            row.createCell(j).setCellValue(String.valueOf(accountList.get(i).get("payReturnStatus")));
                        }
                    }
                    else if (j==5){
                        if (null!=accountList.get(i).get("payReturnCode") &&!"".equals(accountList.get(i).get("payReturnCode").toString()) ){
                            row.createCell(j).setCellValue(String.valueOf(accountList.get(i).get("payReturnCode")));
                        }
                    }
                    else if (j==6){
                        if (null!=accountList.get(i).get("payReturnDesc") &&!"".equals(accountList.get(i).get("payReturnDesc").toString()) ){
                            row.createCell(j).setCellValue(String.valueOf(accountList.get(i).get("payReturnDesc")));
                        }
                    }
                    //row.createCell(j).setCellValue(values[i][j]);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (countDownLatch != null) {
                countDownLatch.countDown();
            }
        }
    }
}
